﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Drawing.Printing;
using Microsoft.ReportingServices.Interfaces;
using XJLSC.QM;
using Microsoft.Reporting.WinForms.Internal.Soap.ReportingServices2005.Execution;


namespace SMALLERP.QM
{   
    public partial class QMFQC : Form
    {    PrintDocument printDocument;
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public delegate void SJDDY(object sender, EventArgs e);
        public SJDDY sjddy;
        public int selectionIdx;
        public string shzt="";

        public QMFQC()
        {
            InitializeComponent();
        }




        private void button1_Click_1(object sender, EventArgs e)
        {
            string strSql = null;
            string jypc=null;
            jypc = textBox16.Text.ToString().Substring(4, 4);


            if (radioButton1.Checked)
            {
                strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'select TB004 检验编号,RTRIM(QMSTB.UDF02) 检验类型,RTRIM(QMSTB.UDF01) 检验项目,TB013 检验标准,Convert(decimal(18,2),TB020) 正公差,Convert(decimal(18,2),TB021) 负公差' select @sql1 = @sql1 + ' , max(case TC005 when ''' + TC005 + ''' then Convert(decimal(18,2),QMSTC.UDF51) else 0 end) [' + TC005 + ']' from (select DISTINCT TC005 from QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TCC01='" + jypc + "' AND TC008='" + textBox15.Text + "') as a set @sql1 = @sql1 + ' from QMSTB LEFT JOIN QMSTC ON TB001=TC001 AND TB002=TC002 AND TB003=TC003 AND TB004=TC004 AND TB014=TC008 AND TBC01=TCC01 WHERE TB001=''" + textBox8.Text + "'' AND TB002=''" + textBox9.Text + "'' AND TB003=''" + textBox10.Text + "'' AND (TB021>0 OR TB016=''3'') AND TB014=''" + textBox15.Text + "'' AND TBC01=''" + jypc + "'' group by TB004,RTRIM(QMSTB.UDF01),RTRIM(QMSTB.UDF02),TB013,TB020,TB021 ORDER BY TB004' EXEC(@sql1)";
            }
           
            if (radioButton2.Checked)
            {
                strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'select TB004 检验编号,RTRIM(QMSTB.UDF02) 检验类型,RTRIM(QMSTB.UDF01) 检验项目,TB013 检验标准,Convert(decimal(18,2),TB020) 正公差,Convert(decimal(18,2),TB021) 负公差' select @sql1 = @sql1 + ' , max(case TC005 when ''' + TC005 + ''' then Convert(decimal(18,2),QMSTC.UDF51) else 0 end) [' + TC005 + ']' from (select DISTINCT TC005 from QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TC003='" + textBox10.Text + "' AND TCC01='0001') as a set @sql1 = @sql1 + ' from QMSTB LEFT JOIN QMSTC ON TB001=TC001 AND TB002=TC002 AND TB003=TC003 AND TB004=TC004 AND TB014=TC008 AND TBC01=TCC01 WHERE TB001=''" + textBox8.Text + "'' AND TB002=''" + textBox9.Text + "'' AND TB003=''" + textBox10.Text + "'' AND (TB021>0 OR TB016=''3'') AND TB014=''" + textBox15.Text + "'' AND TBC01=''0001'' group by TB004,RTRIM(QMSTB.UDF01),RTRIM(QMSTB.UDF02),TB013,TB020,TB021 ORDER BY TB004' EXEC(@sql1)";
            }

            if (radioButton3.Checked)
            {
                strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'select TB004 检验编号,RTRIM(QMSTB.UDF02) 检验类型,RTRIM(QMSTB.UDF01) 检验项目,TB013 检验标准,Convert(decimal(18,2),TB020) 正公差,Convert(decimal(18,2),TB021) 负公差' select @sql1 = @sql1 + ' , max(case TC005 when ''' + TC005 + ''' then Convert(decimal(18,2),QMSTC.UDF51) else 0 end) [' + TC005 + ']' from (select DISTINCT TC005 from QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TCC01='" + jypc + "' AND TC008='" + textBox15.Text + "') as a set @sql1 = @sql1 + ' from QMSTB LEFT JOIN QMSTC ON TB001=TC001 AND TB002=TC002 AND TB003=TC003 AND TB004=TC004 AND TB014=TC008 AND TBC01=TCC01 WHERE TB001=''" + textBox8.Text + "'' AND TB002=''" + textBox9.Text + "'' AND TB003=''" + textBox10.Text + "'' AND (TB021>0 OR TB016=''3'') AND TB014=''" + textBox15.Text + "'' AND TBC01=''" + jypc + "'' group by TB004,RTRIM(QMSTB.UDF01),RTRIM(QMSTB.UDF02),TB013,TB020,TB021 ORDER BY TB004' EXEC(@sql1)";
            }
            //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
            try
            {
                DataTable dt;
                 int j = 0;
                    int k = dataGridView1.Columns.Count - 1;
                    if (dataGridView1.Columns.Count - 1 > j)
                    {
                        for (int i = j; i <= k; i++)
                        {

                            dataGridView1.Columns.RemoveAt(j);
                        }
                    }
                dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }

          
                strSql = "select TB004 检验编号,QMSTB.UDF02 AS 检验类别,QMSTB.UDF01 AS 检验项目,TB018 检验结果,ISNULL(QMSTB.UDF51,0) CR,ISNULL(QMSTB.UDF52,0) MAJ,ISNULL(QMSTB.UDF53,0) MIN,TB012 备注 FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "' AND TB016='4' AND TB014='" + textBox15.Text + "' AND TBC01=SUBSTRING('" + textBox16.Text + "',5,4)";
          
                 
                 //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
            try
            {
                DataTable dt;
                //dataGridView2.Columns.Clear();
                dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                dataGridView2.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
           
                string dhszm = null;
                string dbszm = null;
                dhszm = textBox1.Text.Substring(0, 1);
                if (dhszm == "5")
                {
                     dbszm = textBox1.Text.Substring(0, 4);
                     if (dbszm == "5110" || dbszm == "5210" || dbszm=="5160")
                     {
                         radioButton1.Checked = true;
                     }
                     else
                     {
                         radioButton3.Checked = true;
                     }
                }
                if (dhszm == "2")
                {
                    radioButton2.Checked = true;
                }
                textBox11.Text = "0";
                textBox13.Text = "0";
                textBox14.Text = "0";
                textBox18.Text = "0";
                comboBox1.Text = "";
                string strSql = null;
                if (radioButton1.Checked)
                {
                    label13.Visible = true;
                    strSql = "SELECT TA006,MB002,MB003,INVMB.UDF01,TA015,TA001,TA002,'0000' AS XH,CONVERT(CHAR(8),GETDATE(),112) AS TA003 FROM MOCTA LEFT JOIN INVMB ON TA006=MB001 WHERE TA001+'-'+TA002='" + textBox1.Text + "'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            textBox3.Text = sdr1["TA006"].ToString();
                            textBox4.Text = sdr1["MB002"].ToString();
                            textBox5.Text = sdr1["MB003"].ToString();
                            textBox6.Text = sdr1["UDF01"].ToString();
                            textBox8.Text = sdr1["TA001"].ToString();
                            textBox9.Text = sdr1["TA002"].ToString();
                            textBox10.Text = sdr1["XH"].ToString();
                            textBox16.Text = sdr1["TA003"].ToString();
                            textBox7.Text = Convert.ToDouble(sdr1["TA015"]).ToString();
                            textBox2.Text = "10";
                            textBox1.Text = "";
                        }
                        if (sdr1.HasRows == false)
                        {
                            MessageBox.Show("该单号不存在！", "软件提示");
                            //textBox1.Text = "";
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }
                if (radioButton2.Checked)
                {
                    label13.Visible = false;
                    strSql = "SELECT TO007,MB002,MB003,INVMB.UDF01,TO024,TO001,TO002,TO003,TN003 FROM COPTO LEFT JOIN COPTN ON TO001=TN001 AND TO002=TN002 LEFT JOIN INVMB ON TO007=MB001 WHERE TO001+'-'+TO002+'-'+TO003='" + textBox1.Text + "'";

                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            textBox3.Text = sdr1["TO007"].ToString();
                            textBox4.Text = sdr1["MB002"].ToString();
                            textBox5.Text = sdr1["MB003"].ToString();
                            textBox6.Text = sdr1["UDF01"].ToString();
                            textBox8.Text = sdr1["TO001"].ToString();
                            textBox9.Text = sdr1["TO002"].ToString();
                            textBox10.Text = sdr1["TO003"].ToString();
                            textBox16.Text = sdr1["TN003"].ToString();
                            textBox7.Text = Convert.ToDouble(sdr1["TO024"]).ToString();
                            textBox2.Text = "10";
                            textBox1.Text = "";
                        }
                        if (sdr1.HasRows == false)
                        {
                            MessageBox.Show("该单号不存在！", "软件提示");
                            //textBox1.Text = "";
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }

                if (radioButton3.Checked)
                {
                    label13.Visible = true;
                    strSql = "SELECT TG004,TG005,TG006,INVMB.UDF01,TG011,TG001,TG002,TG003 AS XH,CONVERT(CHAR(8),GETDATE(),112) AS TG003,TG022 FROM MOCTG LEFT JOIN INVMB ON TG004=MB001 WHERE TG001+'-'+TG002+'-'+TG003='" + textBox1.Text + "'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            textBox3.Text = sdr1["TG004"].ToString();
                            textBox4.Text = sdr1["TG005"].ToString();
                            textBox5.Text = sdr1["TG006"].ToString();
                            textBox6.Text = sdr1["UDF01"].ToString();
                            textBox8.Text = sdr1["TG001"].ToString();
                            textBox9.Text = sdr1["TG002"].ToString();
                            textBox10.Text = sdr1["XH"].ToString();
                            textBox16.Text = sdr1["TG003"].ToString();
                            textBox7.Text = Convert.ToDouble(sdr1["TG011"]).ToString();
                            textBox2.Text = "10";
                            textBox1.Text = "";
                            shzt = sdr1["TG002"].ToString().Trim();
                        }
                        if (sdr1.HasRows == false)
                        {
                            MessageBox.Show("该单号不存在！", "软件提示");
                            //textBox1.Text = "";
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }

                string jypc = null;
                jypc = textBox16.Text.ToString().Substring(4, 4);
                strSql = "SELECT ISNULL(right('000'+rtrim(cast(cast(MAX(TA026)+1 as numeric(16)) as char(4))),4),'0001') JYXH FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "' AND TAC04='" + jypc + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        //textBox15.Text = sdr1["JYXH"].ToString();
                        label13.Text = "今天已检到" + sdr1["JYXH"].ToString() + "批";
                        textBox15.Text = "0001";

                    }

                    if (sdr1.HasRows == false)
                    {
                        return;
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }

                strSql = "SELECT MG003 检验编号,UDF02 AS 检验类型,UDF01 AS 检验项目,MG006 AS 检验标准,Convert(decimal(18,2),MG012) AS 正公差,Convert(decimal(18,2),MG013) AS 负公差 FROM QMSMG WHERE MG002='" + textBox3.Text + "' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    int j = dataGridView1.Columns["负公差"].Index+1;
                    int k = dataGridView1.Columns.Count - 1;
                    if (dataGridView1.Columns.Count - 1 > j)
                    {
                        for (int i = j; i <= k; i++)
                        {

                            dataGridView1.Columns.RemoveAt(j);
                        }
                        
                    }

                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                {
                    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    string xh = "";
                    xh = "0" + i;
                    xh = xh.Substring(xh.Length - 2, 2);
                    acCode.Name = "YB" + xh;
                    acCode.DataPropertyName = "YB" + xh;
                    acCode.HeaderText = xh;
                    dataGridView1.Columns.Add(acCode);
                }
                DataGridViewTextBoxColumn acCodeA = new DataGridViewTextBoxColumn();
                acCodeA.Name = "CR";
                acCodeA.DataPropertyName = "CR";
                acCodeA.HeaderText = "CR";
                dataGridView1.Columns.Add(acCodeA);

                DataGridViewTextBoxColumn acCodeB = new DataGridViewTextBoxColumn();
                acCodeB.Name = "MAJ";
                acCodeB.DataPropertyName = "MAJ";
                acCodeB.HeaderText = "MAJ";
                dataGridView1.Columns.Add(acCodeB);

                DataGridViewTextBoxColumn acCodeC = new DataGridViewTextBoxColumn();
                acCodeC.Name = "MIN";
                acCodeC.DataPropertyName = "MIN";
                acCodeC.HeaderText = "MIN";
                dataGridView1.Columns.Add(acCodeC);

                strSql = "SELECT  MG003 检验编号,MG006 AS 检验类别,UDF02 AS 检验类型,UDF01 AS 检验项目,'' AS 检验结果,0 CR,0 MAJ,0 MIN,'' AS 备注 FROM QMSMG WHERE MG002='文本项目2' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    //dataGridView2.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView2.DataSource = dt;


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                //for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                //{
                //    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                //    acCode.Name = "YB" + i;
                //    acCode.DataPropertyName = "YB" + i;
                //    acCode.HeaderText = "样本" + i;
                //    dataGridView2.Columns.Add(acCode);
                //}
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "DELETE FROM QMSMG WHERE MG002='" + textBox3.Text + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            //strSql = "DELETE FROM QMSMG WHERE MG002='文本项目2'";

            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    return;
            //}

            string jybz = null;
            string jyxm = null;
            string jybh = null;
            string jylx = null;
            double jysx = 0;
            double jyxx = 0;
            
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                jybz = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value.ToString();
                jyxm = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value.ToString();
                jybh = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString();
                jylx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验类型"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验类型"].Index].Value.ToString();
                jysx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                jyxx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value);

                if (jybh != "")
                {
                    strSql = "INSERT INTO QMSMG(COMPANY,CREATOR,CREATE_DATE,FLAG,MG001,MG002,MG003,MG006,MG008,MG009,MG012,MG013,UDF01,UDF02) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','101','" + textBox3.Text + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString() + "','" + jybz + "','****','1','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + jyxm + "','" + jylx + "'";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                            //MessageBox.Show(jybh.ToString(), "软件提示");
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                }
            }

            //for (int i = 0; i <= this.dataGridView2.Rows.Count - 1; i++)
            //{
            //    jybz = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value.ToString();
            //    jyxm = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value.ToString();
            //    jybh = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value.ToString();

            //    //jysx = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value);
            //    //jyxx = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["负公差"].Index].Value);

            //    if (jybh != "")
            //    {
            //        strSql = "INSERT INTO QMSMG(COMPANY,CREATOR,CREATE_DATE,FLAG,MG001,MG002,MG003,MG006,MG008,MG009,MG012,MG013,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','101','文本项目2','" + jybh + "','" + jybz + "','****','2','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + jyxm + "'";

            //        try
            //        {
            //            if (db.ExecDataBySql1(strSql) > 0)
            //            {

            //            }
            //            else
            //            {

            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            return;
            //        }
            //    }
            //}
            MessageBox.Show("保存成功！", "软件提示");
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);



        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                string jybh = (this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? "" : this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString();

                if (jybh == "")
                {
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value = String.Format("{0}", 0);
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["正公差"].Index].Value = "0";
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["负公差"].Index].Value = "0";
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验标准"].Index].Value = "0";
                    int bs = 0;
                    int jybs = 0;
                    for (int i = 0; i <= this.dataGridView1.RowCount - 1; i++)
                    {
                        jybs = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value);

                        if (bs < jybs)
                        {
                            bs = jybs;
                        }

                    }


                    //for (int i = 0; i <= this.dataGridView2.RowCount - 1; i++)
                    //{
                    //    jybs = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value);

                    //    if (bs < jybs)
                    //    {
                    //        bs = jybs;
                    //    }

                    //}
                    string xh = "";
                    xh = "0" + (bs + 1);
                    xh = xh.Substring(xh.Length - 2, 2);
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value = xh.ToString();
                    for (int i = this.dataGridView1.Columns["负公差"].Index + 1; i <= this.dataGridView1.ColumnCount - 1; i++)
                    {
                        this.dataGridView1.CurrentRow.Cells[i].Value = "0";
                    }
                }
            }

            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("你确定要保存吗？", "提示信息", MessageBoxButtons.OKCancel);
            if (result == DialogResult.Cancel)
            {
                return;
            }     //窗体标题栏显示相关信息。
            else
            {
            }

            string strSql = null;
            string jypc = null;
            jypc = textBox16.Text.ToString().Substring(4,4);
            //strSql = "DELETE FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "' AND TAC04='" + textBox15.Text + "'";

            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    return;
            //}

            strSql = "DELETE FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "' AND TB014='" + textBox15.Text + "' AND TBC01='" + jypc + "'";
            //MessageBox.Show(strSql, "软件提示");
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
          
            strSql = "DELETE FROM QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TC003='" + textBox10.Text + "' AND TC008='" + textBox15.Text + "' AND TCC01='" + jypc + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            string jybz = null;
            string jyxm = null;
            string jybh = null;
            string jylx = null;
            string jyjg = null;
            double jysx = 0;
            double jyxx = 0;
            //if (textBox15.Text == "0001")
            //{
                for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
                {
                    jybz = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value.ToString();
                    jyxm = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value.ToString();
                    jybh = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString();
                    jylx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验类型"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验类型"].Index].Value.ToString();
                    jysx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                    jyxx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value);

                    if (jybh != "")
                    {
                        strSql = "INSERT INTO QMSTB(COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB013,TB014,TB015,TB016,TB020,TB021,TBC01,UDF01,UDF02) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString() + "','" + jybz + "','" + textBox15.Text + "','1','3','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + jypc + "','" + jyxm + "','" + jylx + "'";

                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {

                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            return;
                        }
                        double scza = 0.00;
                        string xh = "";
                        string bt = "";
                        for (int j = this.dataGridView1.Columns["负公差"].Index + 1; j <= this.dataGridView1.Columns.Count - 1; j++)
                        {

                            xh = "0" + (j - 4);
                            xh = xh.Substring(xh.Length - 2, 2);
                            scza = (this.dataGridView1.Rows[i].Cells[j].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[j].Value);
                            bt = this.dataGridView1.Columns[j].HeaderText.ToString();

                            strSql = "INSERT INTO QMSTC(COMPANY,CREATOR,CREATE_DATE,FLAG,TC001,TC002,TC003,TC004,TC005,TC008,TCC01,UDF01,UDF51) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + jybh + "','" + bt + "','" + textBox15.Text + "','" + jypc + "',''," + scza + "";

                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {

                                }
                                else
                                {

                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                return;
                            }

                        }
                    }


                //}
            }
            double crjg = 0;
            double majjg = 0;
            double minjg = 0;
            string wbbz = "";
            for (int i = 0; i <= this.dataGridView2.Rows.Count - 1; i++)
            {
                jybz = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value.ToString();
                jyxm = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value.ToString();
                jybh = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value.ToString();
                jyjg = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYJG2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYJG2"].Index].Value.ToString();
                wbbz = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBZ"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBZ"].Index].Value.ToString();

                crjg = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["CR"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["CR"].Index].Value);
                majjg = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["MAJ"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["MAJ"].Index].Value);
                minjg = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["MIN"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["MIN"].Index].Value);

                if (jybh != "")
                {
                    strSql = "INSERT INTO QMSTB(COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB012,TB013,TB014,TB015,TB016,TB018,TB020,TB021,TBC01,UDF01,UDF02,UDF51,UDF52,UDF53) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + jybh + "','" + wbbz + "','" + jybz + "','" + textBox15.Text + "','2','4','" + jyjg + "',0,0,'" + jypc + "','" + jyxm + "','" + jybz + "'," + crjg + "," + majjg + "," + minjg + "";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                }
            }

            if (comboBox1.Text == "允收" || comboBox1.Text == "特采" || comboBox1.Text == "合格" || comboBox1.Text == "拒收" || comboBox1.Text == "选别" || comboBox1.Text == "返工")
            {
                if (Convert.ToDouble(textBox7.Text) < (Convert.ToDouble(textBox11.Text) + Convert.ToDouble(textBox13.Text) + Convert.ToDouble(textBox14.Text)) && comboBox1.Text != "返工")
                {
                    MessageBox.Show("验收数+破坏数+报废数不能大于到货数，请核查！", "软件提示");
                    return;
                }
                if (shzt == "Y")
                {
                    return;
                }

                strSql = "SELECT RTRIM(UDF01) AS JYJG FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "' AND TA026='" + textBox15.Text + "' AND TAC04='" + jypc + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        jyjg = sdr1["JYJG"].ToString();
                    }

                    if (sdr1.HasRows == true)
                    {
                        MessageBox.Show("已经判定为" + jyjg + ",不允许重复判定！", "软件提示");
                        return;
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }

                strSql = "UPDATE MOCTG SET TG013=" + textBox11.Text + ",TG038=" + textBox11.Text + ",TG032=" + textBox13.Text + ",TG012=" + textBox14.Text + ",TG023=TG011-" + textBox11.Text + "-" + textBox13.Text + "-" + textBox14.Text + ",TG016='Y' WHERE TG001='" + textBox8.Text + "' AND TG002='" + textBox9.Text + "' AND TG003='" + textBox10.Text + "'";
              
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                strSql = "INSERT INTO QMSTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA022,TA023,TA024,TA025,TA026,TA027,TA028,TA029,TA030,TA031,TA032,TA033,TA034,TA035,TA036,TA037,TA038,TA039,TA040,TA041,TA042,TA043,TA044,TA045,TAC01,TAC02,TAC03,TAC04,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox16.Text + "','" + textBox16.Text + "','" + PropertyClass.OperatorCode + "'," + textBox13.Text + ",0,0,'',";
                strSql = strSql + "'2','2',0,'Y','" + textBox19.Text + "','','','',CONVERT(CHAR(8),GETDATE(),112),'" + PropertyClass.OperatorCode + "','N'," + textBox2.Text + ",0,0,0,'" + textBox15.Text + "','到货验收单PURCAI02',0,'" + textBox16.Text + "','" + textBox17.Text + "',0,0,0,'N','','','','','',NULL,0,0,0,0,0," + textBox11.Text + ",0,0,'" + jypc + "','" + comboBox1.Text + "'";
                //textBox18.Text = strSql;
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                string jhph = "";
                strSql = "SELECT RTRIM(CC005)+SUBSTRING(CONVERT(CHAR(8),GETDATE(),112),3,6) AS JYPH FROM PURCD LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        jhph = sdr1["JYPH"].ToString();
                    }

                    if (sdr1.HasRows == true)
                    {

                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }

                strSql = "INSERT INTO INVME(FLAG,ME001,ME002,ME003,ME004,ME005,ME006,ME007,ME008,ME009,ME010,ME011) SELECT '1','" + textBox3.Text + "','" + jhph + "','','','" + textBox8.Text + "','" + textBox9.Text + "','N','','','','' WHERE NOT EXISTS(SELECT * FROM INVME WHERE ME001='" + textBox3.Text + "' AND ME002='" + jhph + "')";

                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                double YTSL = Convert.ToDouble(textBox7.Text) - (Convert.ToDouble(textBox11.Text) + Convert.ToDouble(textBox13.Text) + Convert.ToDouble(textBox14.Text));
                strSql = "UPDATE PURCD SET CD018=" + textBox11.Text + ",CD019=" + YTSL + ",CD026=" + textBox14.Text + ",CD028=" + textBox13.Text + " WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                //textBox18.Text = strSql;
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

            }
            MessageBox.Show("保存成功！", "软件提示");


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "特采")
            {

            }
            else
            {

            }

            if (comboBox1.Text == "允收" || comboBox1.Text == "特采" || comboBox1.Text == "合格" || comboBox1.Text == "选别")
            {
                textBox11.Text = textBox7.Text;
            }
            else
            {
                textBox11.Text = "0";
            }
        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void QMSJD_Load(object sender, EventArgs e)
        {
            textBox11.Text = "0";
            textBox7.Text = "0";
            label13.Visible = false;
            string yf = "0" + System.DateTime.Today.Month.ToString();
            yf = yf.Substring(yf.Length - 2, 2);
            string rq = "0" + System.DateTime.Today.Day.ToString();
            rq = rq.Substring(rq.Length-2,2);
            textBox16.Text = System.DateTime.Today.Year.ToString() + yf + rq;
            //radioButton2.Checked = true;
            textBox13.Text = "0";
            textBox14.Text = "0";
            comboBox1.Text = "";
            if (JYCX.LX == "FQC")
            {
                radioButton1.Checked = true;
                //textBox1.Text = JYCX.DB + "-" + JYCX.DH + "-" + JYCX.XH;
                //textBox1_KeyDown(null, null);
                //textBox1.Text += "textBox1_KeyDown" + System.Environment.NewLine;
                textBox8.Text = JYCX.DB;
                textBox9.Text = JYCX.DH;
                textBox10.Text = JYCX.XH;
                textBox15.Text = JYCX.CS;
                textBox16.Text = JYCX.JYRQ;
                button1.PerformClick();
            }

            if (JYCX.LX == "OQC")
            {
                radioButton2.Checked = true;
                textBox1.Text = JYCX.DB + "-" + JYCX.DH + "-" + JYCX.XH;
                //textBox1_KeyDown(null, null);
                //textBox1.Text += "textBox1_KeyDown" + System.Environment.NewLine;
                textBox8.Text = JYCX.DB;
                textBox9.Text = JYCX.DH;
                textBox10.Text = JYCX.XH;
                textBox15.Text = JYCX.CS;
                textBox16.Text = JYCX.JYRQ;
                button1.PerformClick();
            }

            if (JYCX.LX == "PQC")
            {
                radioButton3.Checked = true;
                //textBox1.Text = JYCX.DB + "-" + JYCX.DH + "-" + JYCX.XH;
                //textBox1_KeyDown(null, null);
                //textBox1.Text += "textBox1_KeyDown" + System.Environment.NewLine;
                textBox8.Text = JYCX.DB;
                textBox9.Text = JYCX.DH;
                textBox10.Text = JYCX.XH;
                textBox15.Text = JYCX.CS;
                textBox16.Text = JYCX.JYRQ;
                button1.PerformClick();
            }

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            string strSql = null;
            if (e.KeyCode == Keys.Enter)
            {
                strSql = "SELECT MG003 检验编号,UDF02 AS 检验类型,UDF01 AS 检验项目,MG006 AS 检验标准,MG012 AS 正公差,MG013 AS 负公差 FROM QMSMG WHERE MG002='" + textBox3.Text + "' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    dataGridView1.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                {
                    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    string xh = "";
                    xh = "0" + i;
                    xh = xh.Substring(xh.Length - 2, 2);
                    acCode.Name = "YB" + xh;
                    acCode.DataPropertyName = "YB" + xh;
                    acCode.HeaderText = "样本" + xh;
                    dataGridView1.Columns.Add(acCode);
                }

                strSql = "SELECT MG003 检验编号,UDF02 AS 检验类型,UDF01 AS 检验项目,MG006 AS 检验标准,MG012 AS 正公差,MG013 AS 负公差 FROM QMSMG WHERE MG002='文本项目2' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    dataGridView2.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView2.DataSource = dt;


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                //for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                //{
                //    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                //    acCode.Name = "YB" + i;
                //    acCode.DataPropertyName = "YB" + i;
                //    acCode.HeaderText = "样本" + i;
                //    dataGridView2.Columns.Add(acCode);
                //}

            }
        }

        private void textBox10_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox11.Text = "0";
                textBox13.Text = "0";
                textBox14.Text = "0";
                textBox18.Text = "0";
                comboBox1.Text = "";
                string strSql = null;
                if (radioButton1.Checked)
                {
                    label13.Visible = true;
                    strSql = "SELECT TA006,MB002,MB003,INVMB.UDF01,TA015,TA001,TA002,'0000' AS XH,CONVERT(CHAR(8),GETDATE(),112) AS TA003 FROM MOCTA LEFT JOIN INVMB ON TA006=MB001 WHERE TA001+'-'+TA002='" + textBox1.Text + "'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            textBox3.Text = sdr1["TA006"].ToString();
                            textBox4.Text = sdr1["MB002"].ToString();
                            textBox5.Text = sdr1["MB003"].ToString();
                            textBox6.Text = sdr1["UDF01"].ToString();
                            textBox8.Text = sdr1["TA001"].ToString();
                            textBox9.Text = sdr1["TA002"].ToString();
                            textBox10.Text = sdr1["XH"].ToString();
                            textBox16.Text = sdr1["TA003"].ToString();
                            textBox7.Text = Convert.ToDouble(sdr1["TA015"]).ToString();
                            textBox2.Text = "10";
                            textBox1.Text = "";
                        }
                        if (sdr1.HasRows == false)
                        {
                            MessageBox.Show("该单号不存在！", "软件提示");
                            //textBox1.Text = "";
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }

                if (radioButton2.Checked)
                {
                    label13.Visible = false;
                    strSql = "SELECT TO007,MB002,MB003,INVMB.UDF01,TO024,TO001,TO002,TO003,TN003 FROM COPTO LEFT JOIN COPTN ON TO001=TN001 AND TO002=TN002 LEFT JOIN INVMB ON TO007=MB001 WHERE TO001='" + textBox8.Text + "' AND TO002='" + textBox9.Text + "' AND TO003='" + textBox10.Text +"'";

                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            textBox3.Text = sdr1["TO007"].ToString();
                            textBox4.Text = sdr1["MB002"].ToString();
                            textBox5.Text = sdr1["MB003"].ToString();
                            textBox6.Text = sdr1["UDF01"].ToString();
                            textBox8.Text = sdr1["TO001"].ToString();
                            textBox9.Text = sdr1["TO002"].ToString();
                            textBox10.Text = sdr1["TO003"].ToString();
                            textBox16.Text = sdr1["TN003"].ToString();
                            textBox7.Text = Convert.ToDouble(sdr1["TO024"]).ToString();
                            textBox2.Text = "10";
                            textBox1.Text = "";
                        }
                        if (sdr1.HasRows == false)
                        {
                            MessageBox.Show("该单号不存在！", "软件提示");
                            //textBox1.Text = "";
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }

                if (radioButton3.Checked)
                {
                    label13.Visible = true;
                    strSql = "SELECT TG004,TG005,TG006,INVMB.UDF01,TG011,TG001,TG002,TG003 AS XH,CONVERT(CHAR(8),GETDATE(),112) AS TG003,TG022 FROM MOCTG LEFT JOIN INVMB ON TG004=MB001 WHERE TG001='" + textBox8.Text + "' AND TG002='" + textBox9.Text + "' AND TG003='" + textBox10.Text + "' AND TG022='N'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            textBox3.Text = sdr1["TG004"].ToString();
                            textBox4.Text = sdr1["TG005"].ToString();
                            textBox5.Text = sdr1["TG006"].ToString();
                            textBox6.Text = sdr1["UDF01"].ToString();
                            textBox8.Text = sdr1["TG001"].ToString();
                            textBox9.Text = sdr1["TG002"].ToString();
                            textBox10.Text = sdr1["XH"].ToString();
                            textBox16.Text = sdr1["TG003"].ToString();
                            textBox7.Text = Convert.ToDouble(sdr1["TG011"]).ToString();
                            textBox2.Text = "10";
                            textBox1.Text = "";
                            shzt = sdr1["TG022"].ToString().Trim();
                        }
                        if (sdr1.HasRows == false)
                        {
                            MessageBox.Show("该单号不存在！", "软件提示");
                            //textBox1.Text = "";
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }

                string jypc = null;
                jypc = textBox16.Text.ToString().Substring(4, 4);

                strSql = "SELECT ISNULL(right('000'+rtrim(cast(cast(MAX(TA026) as numeric(16))+1 as char(4))),4),'0001') JYXH FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "' AND TAC04='" + jypc + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        //textBox15.Text = sdr1["JYXH"].ToString();
                        label13.Text = "今天已检到" + sdr1["JYXH"].ToString() + "批";
                        textBox15.Text = "0001";

                    }

                    if (sdr1.HasRows == false)
                    {
                        return;
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }

                strSql = "SELECT MG003 检验编号,UDF02 AS 检验类型,UDF01 AS 检验项目,MG006 AS 检验标准,Convert(decimal(18,2),MG012) AS 正公差,Convert(decimal(18,2),MG013) AS 负公差 FROM QMSMG WHERE MG002='" + textBox3.Text + "' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    //if (dataGridView1.Columns.Count - 1 > dataGridView1.Columns["负公差"].Index)
                    //{
                    //    for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                    //    {
                    //        DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    //        string xh = "";
                    //        xh = "0" + i;
                    //        xh = xh.Substring(xh.Length - 2, 2);
                    //        dataGridView1.Columns.Remove("YB" + xh);
                    //    }
                    //    dataGridView1.Columns.Remove("CR");
                    //    dataGridView1.Columns.Remove("MAJ");
                    //    dataGridView1.Columns.Remove("MIN");
                    //}

                    int j = dataGridView1.Columns["负公差"].Index + 1;
                    int k = dataGridView1.Columns.Count - 1;
                    if (dataGridView1.Columns.Count - 1 > j)
                    {
                        for (int i = j; i <= k; i++)
                        {

                            dataGridView1.Columns.RemoveAt(j);
                        }

                    }

                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                {
                    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    string xh = "";
                    xh = "0" + i;
                    xh = xh.Substring(xh.Length - 2, 2);
                    acCode.Name = "YB" + xh;
                    acCode.DataPropertyName = "YB" + xh;
                    acCode.HeaderText = xh;
                    dataGridView1.Columns.Add(acCode);
                }
                DataGridViewTextBoxColumn acCodeA = new DataGridViewTextBoxColumn();
                acCodeA.Name = "CR";
                acCodeA.DataPropertyName = "CR";
                acCodeA.HeaderText = "CR";
                dataGridView1.Columns.Add(acCodeA);

                DataGridViewTextBoxColumn acCodeB = new DataGridViewTextBoxColumn();
                acCodeB.Name = "MAJ";
                acCodeB.DataPropertyName = "MAJ";
                acCodeB.HeaderText = "MAJ";
                dataGridView1.Columns.Add(acCodeB);

                DataGridViewTextBoxColumn acCodeC = new DataGridViewTextBoxColumn();
                acCodeC.Name = "MIN";
                acCodeC.DataPropertyName = "MIN";
                acCodeC.HeaderText = "MIN";
                dataGridView1.Columns.Add(acCodeC);

                strSql = "SELECT  MG003 检验编号,MG006 AS 检验类别,UDF02 AS 检验类型,UDF01 AS 检验项目,'' AS 检验结果,0 CR,0 MAJ,0 MIN,'' AS 备注 FROM QMSMG WHERE MG002='文本项目2' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    //dataGridView2.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView2.DataSource = dt;


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                //for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                //{
                //    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                //    acCode.Name = "YB" + i;
                //    acCode.DataPropertyName = "YB" + i;
                //    acCode.HeaderText = "样本" + i;
                //    dataGridView2.Columns.Add(acCode);
                //}
            }

        }

        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string jybh = (this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? "" : this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value.ToString();

                if (e.ColumnIndex == this.dataGridView2.Columns["JYXM2"].Index && jybh == "")
                {
                    this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value = String.Format("{0}", 0);
                    //this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["正公差"].Index].Value = "0";
                    //this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["负公差"].Index].Value = "0";
                    int bs = 0;
                    int jybs = 0;
                    //for (int i = 0; i <= this.dataGridView1.RowCount - 1; i++)
                    //{
                    //    jybs = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value);

                    //    if (bs < jybs)
                    //    {
                    //        bs = jybs;
                    //    }

                    //}


                    for (int i = 0; i <= this.dataGridView2.RowCount - 1; i++)
                    {
                        jybs = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value);

                        if (bs < jybs)
                        {
                            bs = jybs;
                        }

                    }

                    this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value = String.Format("{0}", bs + 1);

                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //return;
            }


        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView2.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView2.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView2.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);

        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {

                if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["负公差"].Index)
                {
                    double sxz = 0.00;
                    double xxz = 0.00;
                    double bbz = 0.00;
                    sxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                    xxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value);
                    bbz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value);

                    double scz = (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

                    if (scz != 0 && (scz > (bbz + sxz + 0.000001) || scz < (bbz - xxz - 0.000001)))
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //return;
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView1.CurrentRow.Index;
            this.dataGridView1.Rows.RemoveAt(i);
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        //若行已是选中状态就不再进行设置
                        if (dataGridView1.Rows[e.RowIndex].Selected == false)
                        {
                            //dataGridView1.ClearSelection();
                            //dataGridView1.Rows[e.RowIndex].Selected = true;
                        }
                        //只选中一行时设置活动单元格
                        if (dataGridView1.SelectedRows.Count == 1)
                        {
                            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        //弹出操作菜单
                        contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }

            if (e.RowIndex >= 0)
                selectionIdx = e.RowIndex; 
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["负公差"].Index)
                {
                    double sxz = 0.00;
                    double xxz = 0.00;
                    double bbz = 0.00;
                    sxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                    xxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value);
                    bbz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value);

                    double scz = (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

                    if (scz != 0 && (scz > (bbz + sxz + 0.000001) || scz < (bbz - xxz - 0.000001)))
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
                    }

                    if (scz != 0 && scz <= (bbz + sxz) && scz >= (bbz - xxz))
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Black;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                //this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                return;
            }
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView2.CurrentRow.Index;
            this.dataGridView2.Rows.RemoveAt(i);
        }

        private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        //若行已是选中状态就不再进行设置
                        if (dataGridView2.Rows[e.RowIndex].Selected == false)
                        {
                            dataGridView2.ClearSelection();
                            dataGridView2.Rows[e.RowIndex].Selected = true;
                        }
                        //只选中一行时设置活动单元格
                        if (dataGridView2.SelectedRows.Count == 1)
                        {
                            //dataGridView2.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        //弹出操作菜单
                        contextMenuStrip2.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取剪切板的内容，并按行分割
                string pasteText = "";
                pasteText = Clipboard.GetText();
                if (string.IsNullOrEmpty(pasteText))
                    return;
                if (pasteText == "pasteText")
                {
                    return;
                }
                int tnum = 0;
                int nnum = 0;
                //获得当前剪贴板内容的行、列数
                for (int i = 0; i < pasteText.Length; i++)
                {
                    if (pasteText.Substring(i, 1) == "\t")
                    {
                        tnum++;
                    }
                    if (pasteText.Substring(i, 1) == "\n")
                    {
                        nnum++;
                    }
                }
                Object[,] data;
                //粘贴板上的数据来自于EXCEL时，每行末都有\n，在DATAGRIDVIEW内复制时，最后一行末没有\n
                if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
                {
                    nnum = nnum - 1;
                }
                tnum = tnum / (nnum + 1);
                data = new object[nnum + 1, tnum + 1];//定义一个二维数组
                String rowstr;
                rowstr = "";
                //MessageBox.Show(pasteText.IndexOf("B").ToString());
                //对数组赋值
                for (int i = 0; i < (nnum + 1); i++)
                {
                    for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                    {
                        //一行中的最后一列
                        if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
                        {
                            rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
                        }
                        //最后一行的最后一列
                        if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
                        {
                            rowstr = pasteText.Substring(0);
                        }
                        //其他行列
                        if (colIndex != tnum)
                        {
                            rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
                            pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
                        }
                        data[i, colIndex] = rowstr;
                    }
                    //截取下一行数据
                    pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);
                }
                //获取当前选中单元格所在的列序号
                int curntindex = dataGridView1.CurrentRow.Cells.IndexOf(dataGridView1.CurrentCell);
                //获取获取当前选中单元格所在的行序号
                int rowindex = dataGridView1.CurrentRow.Index;
                //MessageBox.Show(curntindex.ToString ());
                for (int j = 0; j < (nnum + 1); j++)
                {
                    for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                    {
                        if (!dataGridView1.Columns[colIndex + curntindex].Visible)
                        {
                            continue;
                        }
                        if (!dataGridView1.Rows[j + rowindex].Cells[colIndex + curntindex].ReadOnly)
                        {
                            dataGridView1.Rows[j + rowindex].Cells[colIndex + curntindex].Value = data[j, colIndex];
                        }
                    }
                }
                Clipboard.Clear();
            }
            catch
            {
                Clipboard.Clear();
                //MessageBox.Show("粘贴区域大小不一致");
                return;
            }
        }

        public void DataGridViewEnableCopy(DataGridView p_Data)
        {
            Clipboard.SetData(DataFormats.Text, p_Data.GetClipboardContent());
        }

        

    
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.dataGridView1.ClipboardCopyMode= DataGridViewClipboardCopyMode.EnableWithoutHeaderText; //设置可复制的模式 

            Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent());
            
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.V)
            {

                try
                {
                    // 获取剪切板的内容，并按行分割   
                    string pasteText = Clipboard.GetText();
                    if (string.IsNullOrEmpty(pasteText))
                        return;
                    int tnum = 0;
                    int nnum = 0;
                    //获得当前剪贴板内容的行、列数
                    for (int i = 0; i < pasteText.Length; i++)
                    {
                        if (pasteText.Substring(i, 1) == "\t")
                        {
                            tnum++;
                        }
                        if (pasteText.Substring(i, 1) == "\n")
                        {
                            nnum++;
                        }
                    }
                    Object[,] data;
                    //粘贴板上的数据来自于EXCEL时，每行末都有\n，在DATAGRIDVIEW内复制时，最后一行末没有\n
                    if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
                    {
                        nnum = nnum - 1;
                    }
                    tnum = tnum / (nnum + 1);
                    data = new object[nnum + 1, tnum + 1];//定义一个二维数组
                    String rowstr;
                    rowstr = "";
                    //MessageBox.Show(pasteText.IndexOf("B").ToString());
                    //对数组赋值
                    for (int i = 0; i < (nnum + 1); i++)
                    {
                        for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                        {
                            //一行中的最后一列
                            if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
                            {
                                rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
                            }
                            //最后一行的最后一列
                            if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
                            {
                                rowstr = pasteText.Substring(0);
                            }
                            //其他行列
                            if (colIndex != tnum)
                            {
                                rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
                                pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
                            }
                            data[i, colIndex] = rowstr;
                        }
                        //截取下一行数据
                        pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);
                    }
                    //获取当前选中单元格所在的列序号
                    int curntindex = dataGridView1.CurrentRow.Cells.IndexOf(dataGridView1.CurrentCell);
                    //获取获取当前选中单元格所在的行序号
                    int rowindex = dataGridView1.CurrentRow.Index;
                    //MessageBox.Show(curntindex.ToString ());
                    for (int j = 0; j < (nnum + 1); j++)
                    {
                        for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                        {
                            dataGridView1.Rows[j + rowindex].Cells[colIndex + curntindex].Value = data[j, colIndex];
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("粘贴区域大小不一致");
                    return;
                }
                
            }
        }
  
        private void button4_Click(object sender, EventArgs e)
        {
            string x0 = "";
            string x1 = "";
            string x2 = "";
            string x3 = "";
            string x4 = "";
            string x5 = "";
            string x6 = "";
            string x7 = "";
            double x8 = 0;
            double x9 = 0;
            string x10 = "";
            string x11 = "";
            string x12 = "";
            if (radioButton1.Checked)
            {
                x0 = "FQC";
            }

            if (radioButton2.Checked)
            {
                x0 = "OQC";
            }
            x1 = textBox8.Text.ToString();
            x2 = textBox9.Text.ToString();
            x3 = textBox10.Text.ToString();
            x4 = textBox19.Text.ToString();
            x5 = textBox4.Text.ToString();
            x6 = textBox6.Text.ToString();
            x7 = textBox10.Text.ToString();
            x8 = Convert.ToDouble(textBox7.Text);
            x9 = Convert.ToDouble(textBox11.Text);
            x10 = PropertyClass.OperatorName;
            x11 = textBox15.Text.ToString();
            x12 = textBox16.Text.ToString().Substring(4, 4);
            QMFOQC xx = new QMFOQC(x0,x1, x2, x3, x4, x5, x6, x7,x8,x9,x10,x11,x12);
            OQCprint f = new OQCprint();
            f.cdgzzx += new OQCprint.Cdgzzx(textBox1_TextChanged);
            f.Show();
        }

        void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            //设置字体
            Font f = new Font("宋体", 20);
            //控制打印是什么内容 label1.text是打印内容 f是字体对象 brushes是填充颜色 10,10是打印到纸上的起始坐标
            e.Graphics.DrawString("旺财你好", f, Brushes.Black, 10, 10);
        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("你确定要删除吗？", "提示信息", MessageBoxButtons.OKCancel);
            if (result == DialogResult.Cancel)
            {
                return;
            }     //窗体标题栏显示相关信息。
            else
            {
            }

            string strSql = null;
            string jypc = null;
            jypc = textBox16.Text.ToString().Substring(4, 4);
            strSql = "DELETE FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "' AND TA026='" + textBox15.Text + "' AND TAC04='" + jypc + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }

            strSql = "DELETE FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "' AND TB014='" + textBox15.Text + "' AND TBC01='" + jypc + "'";
            //MessageBox.Show(strSql, "软件提示");
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }

            strSql = "DELETE FROM QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TC003='" + textBox10.Text + "' AND TC008='" + textBox15.Text + "' AND TCC01='" + jypc + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            MessageBox.Show("删除成功！", "软件提示");
        }

        private void dataGridView_DragDrop(object sender, DragEventArgs e)
        {
            int idx = GetRowFromPoint(e.X, e.Y);

            if (idx < 0) return;

            if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                DataGridViewRow row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                dataGridView1.Rows.Remove(row);
                dataGridView1.Rows.Insert(idx, row);
                selectionIdx = idx;
            }
        }

        private int GetRowFromPoint(int x, int y)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                Rectangle rec = dataGridView1.GetRowDisplayRectangle(i, false);

                if (dataGridView1.RectangleToScreen(rec).Contains(x, y))
                    return i;
            }

            return -1;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if ((dataGridView1.Rows.Count > 0) && (dataGridView1.SelectedRows.Count > 0) && (dataGridView1.SelectedRows[0].Index != selectionIdx))
            {
                if (dataGridView1.Rows.Count <= selectionIdx)
                    selectionIdx = dataGridView1.Rows.Count - 1;
                dataGridView1.Rows[selectionIdx].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[selectionIdx].Cells[0];
            } 
        }

        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move; 
        }

        private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.Clicks < 2) && (e.Button == MouseButtons.Left))
            {
                if ((e.ColumnIndex == -1) && (e.RowIndex > -1))
                    dataGridView1.DoDragDrop(dataGridView1.Rows[e.RowIndex], DragDropEffects.Move);
            } 
        }

        

    }
}
